<?php 


require "../common/Curl.php";

/**
 *	https://open.esign.cn/doc/detail?id=opendoc%2Fpaas_api%2Fsgv49u&namespace=opendoc%2Fpaas_api
 * 	电子签名SaaS API非标版
 * 	文件管理API----接口顺序排列封装
 *  使用请求签名鉴权方式
 *  下一个------------------ 模板管理 template.php
 */


// 获取文件contentMd5
function getContentBase64Md5($filePath){
    //获取文件MD5的128位二进制数组
    $md5file = md5_file($filePath,true);
    //计算文件的Content-MD5
    $contentBase64Md5 = base64_encode($md5file);
    return $contentBase64Md5;
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// 文件上传两个步骤
// 步骤一：获取文件上传地址

function getUploadUrl($contentMd5,$contentType,$convert2Pdf,$fileName,$fileSize,$appId,$secret,$url){
	$urlss = $url."/v1/files/getUploadUrl";
	$head_url = "/v1/files/getUploadUrl";
	$data = [
		'contentMd5'=>$contentMd5,
		"contentType"=>$contentType,
	    "convert2Pdf"=>$convert2Pdf,
	    "fileName"=>$fileName,
	    "fileSize"=>$fileSize
	];
	$res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}





// 步骤二 文件流上传
function fielUpload($filePath,$getUploadUrl,$contentMd5,$contentType){
	$urlss = $getUploadUrl;
	$res = file_get_contents($filePath);
	$header = array(
        'Content-MD5:'.$contentMd5,
        'Content-Type:'.$contentType
    );
    $result = sendHttp($method="PUT",$urlss,$header,$res);
    return $result;
}


// $filePath = "./wxhead.png";
// $contentMd5 = getContentBase64Md5($filePath);
// $contentType = "application/octet-stream";
// $convert2Pdf = false;
// $fileName = "wxhead.png";
// $fileSize = 25212;

// $a = getUploadUrl($contentMd5,$contentType,$convert2Pdf,$fileName,$fileSize,$appId,$secret,$url);
// var_dump($a);
// $getUploadUrl = json_decode($a,true)['data']['uploadUrl'];   // 获取生成的url
// $b = fielUpload($filePath,$getUploadUrl,$contentMd5,$contentType);
// var_dump($b);die;



//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectFileStatus 查询文件上传状态]
 * @param  [type] $fileId [文件id]
 * @return [json]         [文件状态
0-文件未上传；
1-文件上传中 ；
2-文件上传已完成；
3-文件上传失败 ；
4-文件等待转pdf ；
5-文件已转换pdf ；
6-加水印中；
7-加水印完毕；
8-文件转换中；
9-文件转换失败]
 */
function selectFileStatus($fileId,$appId,$secret,$url){
	$urlss = $url."/v1/files/".$fileId."/status";
	$head_url = "/v1/files/".$fileId."/status";
	$data = [];
	$res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $fileId = "aacf725655f24c5598fc870799626769";
// $a = selectFileStatus($fileId,$appId,$secret,$url);
// var_dump($a);die;


//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectPdfFileStatus 获取PDF文件详情]
 * @param  [type] $fileId [文件id]
 * @return [json]         [返回详情]
 */
function selectPdfFileStatus($fileId,$appId,$secret,$url){
	$urlss = $url."/v1/files/".$fileId;
	$head_url = "/v1/files/".$fileId;
	$data = [];
	$res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $fileId = "aacf725655f24c5598fc870799626769";
// $a = selectPdfFileStatus($fileId,$appId,$secret,$url);
// var_dump($a);die;


